package demo;

public class Person implements Cloneable{
    //Cloneable是一个空接口 我们也称之为标记接口-->如果类实现了这个接口，表明当前类是可以被克隆的
    public String name;
    public int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //Object类是所有类的父类，super.clone是在调用Object类的clone方法
        return super.clone();
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class TestTwo{
    public static void main(String[] args) throws CloneNotSupportedException{
        Person person = new Person("张三",15);
        //因为克隆方法的返回值是Object类型，而Person是其子类，把父类给子类，向下转型，需要强转
        Person person1 = (Person)person.clone();

        System.out.println(person);
        System.out.println(person1);
    }
}
